from urllib import  request, error
import re
import time
import sched
import os
def get_csdn_blog_list(Delaytime):
    url = 'http://blog.csdn.net/'
    headers = {
           'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                         'Chrome/63.0.3239.108 Safari/537.36'}
    req = request.Request(url=url, headers=headers)
    data = request.urlopen(req, timeout=30).read().decode('utf-8', 'ignore')
    path1 = '<h3 .*?><a href="(.*?)"'
    path2 = '<a href="(.*?)" target="_blank" style="display:block;">'
    path3 = '<a strategy=.*? href="(.*?)" target="_blank">'
    linke1 = re.compile(path1, re.S).findall(data)
    linke2 = re.compile(path2).findall(data)
    linke3 = re.compile(path3, re.S).findall(data)
    alllinke = linke1+linke2+linke3
    alllinke= list(set(alllinke))
    print(alllinke)
    for i in range(0, len(alllinke)):
        try:
            thislink = alllinke[i]
            print(i)
            request.urlretrieve(thislink, 'D:\\python\\csdnpachong\\'+str(i)+'.html')
        except error.URLError as e:
            if hasattr(e, 'code'):
                print(e.code)
            if hasattr(e, 'reason'):
                print(e.reason)
    schedule.enter(Delaytime, 0, get_csdn_blog_list, (Delaytime,))

if __name__ == '__main__':
    schedule = sched.scheduler(time.time, time.sleep)
    delaytime =60
    schedule.enter(0, 0, get_csdn_blog_list, (delaytime,))
    schedule.run()